//
//  HomeDataShared.m
//  BigTyre
//
//  Created by 田涛 on 2017/3/23.
//  Copyright © 2017年 Lemon. All rights reserved.
//

#import "HomeDataShared.h"
#import "HomeModel.h"
#import "Common.h"

#import <AFNetworking.h>

@interface HomeDataShared ()
@property (nonatomic, strong) NSMutableArray *scrollArr;
@property (nonatomic, strong) NSMutableArray *optionArr;
@property (nonatomic, strong) NSMutableArray *productArr;
@end

static HomeDataShared *home = nil;
@implementation HomeDataShared


/** 单例*/
+ (instancetype)sharedData {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        home = [[HomeDataShared alloc] init];
    });
    return home;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    if (!home) {
        home = [super allocWithZone:zone];
    }
    return home;
}

- (id)copy {
    return self;
}

- (id)mutableCopy {
    return self;
}


- (void)getURL:(NSString *)url {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager GET:HOME_URL parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        _scrollArr = [NSMutableArray array];
        _optionArr = [NSMutableArray array];
        _productArr = [NSMutableArray array];
        
        NSArray *json = [(NSDictionary *)responseObject objectForKey:@"json"];
        for (NSDictionary *dic in json) {
            
            HomeModel *model = [[HomeModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            
            if ([model.type isEqualToString:@"scroll_ad"]) {     // 滚动视图
                
                for (NSDictionary *dic2 in model.subClass) {
                    HomeModel_Scroll *model_s = [[HomeModel_Scroll alloc] init];
                    [model_s setValuesForKeysWithDictionary:dic2];
                    [_scrollArr addObject:model_s.img];
                }
            } else if ([model.type isEqualToString:@"option"]) {     // 分类
                
                for (NSDictionary *dic2 in model.subClass) {
                    HomeModel_Option *model_o = [[HomeModel_Option alloc] init];
                    [model_o setValuesForKeysWithDictionary:dic2];
                    [_optionArr addObject:model_o];
                }
            } else if ([model.type isEqualToString:@"product"] || [model.type isEqualToString:@"product2"] || [model.type isEqualToString:@"partner"]) {   //  推荐
                
                for (NSDictionary *dic2 in model.subClass) {
                    HomeModel_Product *model_p = [[HomeModel_Product alloc] init];
                    [model_p setValuesForKeysWithDictionary:dic2];
                    [_productArr addObject:model_p];
                }
            }
        }
        // Block 传值
        if (self.scrollBlock != nil) {
            self.scrollBlock(_scrollArr);
        }
        if (self.optionBlock != nil) {
            self.optionBlock(_optionArr);
        }
        if (self.productBlock != nil) {
            self.productBlock(_productArr);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败 error:%@",error);
    }];
}

@end
